package org.sample.sss.controller;

import org.sample.sss.pojo.Resume;
import org.sample.sss.service.IResumeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/resume")
public class ResumeController {
    @Autowired
    private IResumeService resumeService;

    @RequestMapping(value = "/", method = {RequestMethod.GET, RequestMethod.POST})
    public ModelAndView getAll(){
        List<Resume> allResume = resumeService.getAllResume();

        ModelAndView mv = new ModelAndView();
        mv.addObject("resumes", allResume);
        mv.setViewName("list");
        mv.setStatus(HttpStatus.OK);
        return mv;
    }

    @RequestMapping(value = "/toUpdate")
    public ModelAndView toUpdate(long id){
        Resume resume = resumeService.getOne(id);
        System.out.println("in toupdate resume=" + resume);
        ModelAndView mv = new ModelAndView();
        mv.addObject("resume", resume);
        mv.setViewName("update");
        mv.setStatus(HttpStatus.OK);
        return mv;
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public String update(Resume resume){
        resumeService.save(resume);
        return "redirect:/resume/";
    }

    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public String delete(Long id){
        resumeService.deleteOne(id);
        return "redirect:/resume/";
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public String add(Resume resume){
        resumeService.save(resume);
        return "redirect:/resume/";
    }
}
